package com.yanqu.road.server.cmd.assistant;

import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.pb.assistant.AssistantProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.union.cross.CrossUnionModule;
import com.yanqu.road.server.manger.assistant.AssistantMgr;
import com.yanqu.road.server.manger.assistant.handler.ServerActionHandler;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = Protocol.S_CROSS_ASSISTANT_ACTION_FOR_UNION_FROM_CROSS, desc = "处理跨服商会小助理操作")
public class AssistantUnionCrossActionFromCross extends AbstractCommand {

    private AssistantProto.AssistantCrossActionRespMsg.Builder builder;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        builder = AssistantProto.AssistantCrossActionRespMsg.parseFrom(packet.getBytes()).toBuilder();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        // 处理皇城响应
        AssistantProto.AssistantActionReqMsg.Builder assistantReq = builder.getAssistantActionReqMsg().toBuilder();
        AssistantProto.AssistantActionRespMsg.Builder assistantResp = builder.getAssistantActionRespMsg().toBuilder();

        CrossUnionModule unionModule = player.getModule(CrossUnionModule.class);

        for (AssistantProto.AssistantUnionCrossBuildResultTemp resultTemp : builder.getResultList()) {
            String consume = resultTemp.hasConsume() ? resultTemp.getConsume() : "";
            Property reward = unionModule.handleConstructFromCross(resultTemp.getConstructionType(), consume, resultTemp.getRet());
            assistantResp.addCommonResultList(AssistantMgr.builderAssistantCommonResult(resultTemp.getRet(), PropertyHelper.parsePropertyToString(reward), consume, getAssistantNumber(resultTemp.getConstructionType())));
        }

        if(builder.hasOneKeyReceive()){
            AssistantProto.AssistantCommonResult result = builder.getOneKeyReceive();
            if(result.getRet() == 0 && result.hasReward()){
                Property reward = PropertyHelper.parseStringToProperty(result.getReward());
                if (null != reward) {
                    player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.Union, eLogMoneyType.UnionConstructActive);
                }
            }
            assistantResp.addCommonResultList(result);
        }

        if(builder.hasOperate()){
            assistantResp.setOperateRespMsg(builder.getOperate());
        }
        builder.setAssistantActionRespMsg(assistantResp);

        // 本服操作
        new ServerActionHandler().handle(player, assistantReq, assistantResp);
    }

    @Override
    public void sendData(GamePlayer player) {

    }

    public int getAssistantNumber(int constructType){
        if(constructType == 1){
            return AssistantProto.AssistantActionType.UNION_CONSTRUCTION_SIMPLE.getNumber();
        }
        if(constructType == 2){
            return AssistantProto.AssistantActionType.UNION_CONSTRUCTION_MIDDLE.getNumber();
        }
        if(constructType == 3){
            return AssistantProto.AssistantActionType.UNION_CONSTRUCTION_HIGH.getNumber();
        }
        return 0;
    }
}
